<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
    <link rel="stylesheet" type="text/css" media="screen" href="/static/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <div class="row" style="margin-top: 80px">
            <div class="col-md-4 col-md-offset-4">
                <div class="panel panel-primary">
                    <div class="panel-heading">注册</div>
                    <div class="panel-body">
                        <form id="registerForm">
                            <div class="form-group">
                                <label for="username">用户名</label>
                                <input type="text" id="username" name="username" class="form-control" placeholder="请输入用户名">
                            </div>
                            <div class="form-group">
                                <label for="pwd">密码</label>
                                <input type="password" id="pwd" name="password" class="form-control" placeholder="请输入密码">
                            </div>
                            <div class="form-group">
                                <label for="identity">身份</label>
                                <input type="text" id="identity" name="identity" class="form-control" placeholder="请输入身份">
                            </div>
                            <div class="form-inline text-right">
                                <button id="btn" type="button" class="btn btn-primary">注册</button>
                            </div>
                            <div id="error-message" class="text-danger"></div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="./js/axios.min.js"></script>
    <script>
        document.getElementById("btn").onclick = function () {
            const userName = document.getElementById("username").value;
            const password = document.getElementById("pwd").value;
            const identity = document.getElementById("identity").value;

            const url = '/user/register';
            const data = {
                'username': userName,
                'password': password,
                'identity': identity
            };

            axios.post(url, data)
                .then(res => {
                    const R = res.data;
                    if (R.code === 200) {
                        location.href = 'index.html';
                    } else {
                        document.getElementById('error-message').innerText = R.msg;
                    }
                });
        };
    </script>
</body>
</html>
